package com.im.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.im.app.manager.GroupManager;
import com.im.app.mapper.GroupMapper;
import com.im.app.service.IGroupService;
import com.im.app.utils.text.Convert;
import org.jim.core.packets.Group;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IGroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements IGroupService {


    @Resource
    private GroupMapper groupMapper;

    /**
     * 查询群信息
     *
     * @param groupId 群信息主键
     * @return 群信息
     */
    @Override
    public Group selectGroupsByGroupId(String groupId)
    {
        GroupManager manager = GroupManager.getManager();
        Group group = manager.getGroup(groupId);
        if (group != null){
            return group;
        }
        group = groupMapper.selectGroupsByGroupId(groupId);
        if (group != null){
            manager.cacheGroup(groupId,group);
        }
        return group;
    }

    @Override
    public Group selectGroupsById(String id){
        GroupManager manager = GroupManager.getManager();
        Group group = manager.getGroup(id);
        if (group != null){
            return group;
        }
        group = groupMapper.selectGroupsById(id);
        if (group != null){
            manager.cacheGroup(id,group);
        }
        return group;
    }

    /**
     * 查询群信息列表
     *
     * @param group 群信息
     * @return 群信息
     */
    @Override
    public List<Group> selectGroupsList(Group group)
    {
        return groupMapper.selectGroupsList(group);
    }

    /**
     * 新增群信息
     *
     * @param group 群信息
     * @return 结果
     */
    @Override
    public int insertGroups(Group group) {
        return groupMapper.insertGroups(group);
    }

    /**
     * 修改群信息
     *
     * @param group 群信息
     * @return 结果
     */
    @Override
    public int updateGroups(Group group)
    {
        int result = groupMapper.updateGroups(group);
        if (result > 0){
            GroupManager manager = GroupManager.getManager();
            manager.cacheGroup(group.getGroupId(),group);
        }
        return result;
    }

    /**
     * 批量删除群信息
     *
     * @param groupIds 需要删除的群信息主键
     * @return 结果
     */
    @Override
    public int deleteGroupsByGroupIds(String groupIds)
    {
        return groupMapper.deleteGroupsByGroupIds(Convert.toStrArray(groupIds));
    }

    /**
     * 删除群信息信息
     *
     * @param groupId 群信息主键
     * @return 结果
     */
    @Override
    public int deleteGroupsByGroupId(String groupId)
    {
        return groupMapper.deleteGroupsByGroupId(groupId);
    }

    /**
     * 查询群信息
     *
     * @param groupIds
     * @return 结果
     */
    @Override
    public List<Group> selectGroupsByGroupIds(String[] groupIds)
    {
        return groupMapper.selectGroupsByGroupIds(groupIds);
    }


}
